﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml.Serialization;
using Barbar.Colonization.Core;

namespace Barbar.Colonization.Model.RuleSets {
  [XmlType(Namespace = Constants.XmlRootNamespace, TypeName = "TileProduction")]
  public class XmlTileProduction : ITileProduction, IRulesetDeserialization {
    private XmlRawMaterialTypeRef m_RawMaterialTypeRef;
    private IRawMaterialType m_RawMaterialType;
    private int m_Base;
    private int m_PlowBonus;
    private int m_RoadBonus;
    private int m_ExpertBonus;
    private BonusOperationType m_ExpertBonusType;

    public XmlRawMaterialTypeRef RawMaterialTypeRef {
      get { return m_RawMaterialTypeRef; }
      set { m_RawMaterialTypeRef = value; }
    }

    [XmlAttribute()]
    public int Base {
      get { return m_Base; }
      set { m_Base = value; }
    }

    [XmlAttribute()]
    public int PlowBonus {
      get { return m_PlowBonus; }
      set { m_PlowBonus = value; }
    }

    [XmlAttribute()]
    public int RoadBonus {
      get { return m_RoadBonus; }
      set { m_RoadBonus = value; }
    }

    [XmlAttribute()]
    public int ExpertBonus {
      get { return m_ExpertBonus; }
      set { m_ExpertBonus = value; }
    }

    [XmlAttribute()]
    public BonusOperationType ExpertBonusType {
      get { return m_ExpertBonusType; }
      set { m_ExpertBonusType = value; }
    }

    public override string ToString() {
      if (m_RawMaterialType != null)
        return m_RawMaterialType.Name.ToString();
      return m_RawMaterialTypeRef.IdRef;
    }

    #region ITileProduction Members

    IRawMaterialType ITileProduction.RawMaterialType {
      get { return m_RawMaterialType; }
    }

    int ITileProduction.GetProduction(Tile tile, Unit worker) {
      throw new NotImplementedException();
    }

    #endregion

    #region IRulesetDeserialization Members

    void IRulesetDeserialization.OnDeserialization(XmlRuleSet ruleSet) {
      if (ruleSet == null)
        throw new ArgumentNullException("ruleSet");

      foreach(XmlRawMaterialType rawMaterialType in ruleSet.RawMaterialTypes)
        if (rawMaterialType.Id == m_RawMaterialTypeRef.IdRef) {
          m_RawMaterialType = rawMaterialType;
          break;
        }
      if (m_RawMaterialType == null)
        throw new ColonizationException(ExceptionTranslator.RawMaterialTypeNotSet);
    }

    #endregion
  }
}
